package haizhi.fruitmall.action;

import haizhi.fruitmall.constant.AuthMsg;
import haizhi.fruitmall.constant.ResultConstant;
import haizhi.fruitmall.pojo.po.Auth;
import haizhi.fruitmall.service.AuthService;
import haizhi.fruitmall.util.RandomUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/auth")
public class AuthController {
	@Autowired
	private AuthService authService;

	/**
	 * 获取权限操作
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addAuthC", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map addAuthC(@RequestBody Map map) {
		Map messages = new HashMap();
		String auth_c = (String) map.get(AuthMsg.AUTH_C);
		List<Auth> auths = authService.getAuthsByAuthC(auth_c);

		// 判断是否已经存在该权限模块名
		if (auths != null && auths.size() >= 0) {
			messages.put(ResultConstant.RESULT, AuthMsg.AUTH_C_EXIST);
			return messages;
		}

		// 产生唯一的auth_id
		Integer auth_id = Integer.parseInt((String) RandomUtils.generateNum(9));
		while (authService.getAuth(auth_id) != null) {
			auth_id = Integer.parseInt((String) RandomUtils.generateNum(9));
		}

		// 添加Auth
		Auth auth = new Auth();
		auth.setAuthId(auth_id);
		auth.setAuthC(auth_c);
		authService.addAuth(auth);
		messages.put(ResultConstant.RESULT, ResultConstant.SUCCESS);
		return messages;
	}

	@RequestMapping(value = "/addAuthA", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map addAuthA(@RequestBody Map map) {
		Map messages = new HashMap();
		String auth_c = (String) map.get(AuthMsg.AUTH_C);
		String auth_a = (String) map.get(AuthMsg.AUTH_A);
		List<Auth> auths = authService.getAuthsByAuthA(auth_c, auth_a);

		// 判断是否已经存在该权限方法名
		if (auths != null && auths.size() >= 0) {
			messages.put(ResultConstant.RESULT, AuthMsg.AUTH_A_EXIST);
			return messages;
		}

		// 产生唯一的auth_id
		Integer auth_id = Integer.parseInt((String) RandomUtils.generateNum(9));
		while (authService.getAuth(auth_id) != null) {
			auth_id = Integer.parseInt((String) RandomUtils.generateNum(9));
		}

		// 获得父id
		Integer auth_pid = authService.getAuthsByAuthC(auth_c).get(0)
				.getAuthId();

		// 添加Auth
		Auth auth = new Auth();
		auth.setAuthId(auth_id);
		auth.setAuthPid(auth_pid);
		auth.setAuthC(auth_c);
		auth.setAuthA(auth_a);
		authService.addAuth(auth);
		messages.put(ResultConstant.RESULT, ResultConstant.SUCCESS);
		return messages;
	}

	@RequestMapping(value = "/deleteAuthA", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map deleteAuthA(@RequestBody Map map) {
		Map messages = new HashMap();

		// authService.deleteAuthA(.);
		return messages;
	}
}
